\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{prelude}[2020/10/06 AlgoXY Prelude LaTeX package]
%
% loading packages
%

\RequirePackage{ifpdf}
\RequirePackage{ifxetex}

\ifpdf
  \RequirePackage[pdftex,%
       bookmarksnumbered,%
              colorlinks,%
          linkcolor=blue,%
              hyperindex,%
        plainpages=false,%
       pdfstartview=FitH]{hyperref}
\else\ifxetex
  \RequirePackage[bookmarksnumbered,%
               colorlinks,%
           linkcolor=blue,%
               hyperindex,%
         plainpages=false,%
        pdfstartview=FitH]{hyperref}
\else
  \RequirePackage[dvipdfm,%
        bookmarksnumbered,%
               colorlinks,%
           linkcolor=blue,%
               hyperindex,%
         plainpages=false,%
        pdfstartview=FitH]{hyperref}
\fi\fi
%\usepackage{hyperref}

% other packages
%--------------------------------------------------------------------------
\usepackage{graphicx, color}
\usepackage{adjustbox}
\usepackage{wrapfig}
\usepackage{subcaption}  % subfig is deprecated
\usepackage{enumerate}
\usepackage{multicol}
\usepackage{pdfpages}    % to include pdf
\usepackage[table]{xcolor} % for colored table
\usepackage{hhline}
\usepackage{tikz}
\usetikzlibrary{patterns,matrix,positioning,shapes,arrows}

\usepackage{amsmath, amsthm, amssymb, amsbsy, nccmath} % for math
\usepackage{textgreek, upgreek}
\usepackage{gensymb}
\usepackage{mathtools}
\usepackage{extarrows}
\usepackage{exercise} % for exercise
\usepackage{stmaryrd} % for banana brackets
\usepackage{pict2e}   % for customize lens brackets
\usepackage{ulem}

\usepackage{import} % for nested input
\usepackage{standalone}

%
% for programming
%
\usepackage{verbatim}
\usepackage{fancyvrb}
\usepackage{listings}
\usepackage{algorithm} %to remove rules change to \usepackage[plain]{algorithm}
\usepackage[noend]{algpseudocode} %for pseudo code, include algorithmicsx automatically
\usepackage{appendix}
\usepackage{makeidx} % for index support
\usepackage{titlesec}
\usepackage{epigraph}

\usepackage{fontspec}
\usepackage{xunicode}
\usepackage{textcomp}
\usepackage{mdframed}
\usepackage{url}

\usepackage[nomarginpar
  %, margin=.5in
  , top=1.5cm, bottom=1.5cm, left=2cm, right=2cm
]{geometry}

% detect and select Chinese font
% ------------------------------
% fc-list :lang=zh    % list all Chinese fonts
% fc-list :mono       % list all mono fonts
% fc-cache            % refresh cache to load new installed fonts
\def\macmainfont{STSong}  % Under Mac OS X
\def\macboldfont{STHeiti}
\def\macmonofont{Monaco}
\def\winmainfont{SimSun} % Under Windows
\def\winboldfont{SimHei}
\def\winmonofont{Consolas}
\def\linuxmainfont{WenQuanYi Micro Hei} % Under Linux
\def\linuxboldfont{WenQuanYi Zen Hei}
\def\linuxmainfont{Courier}

\suppressfontnotfounderror1 % Avoid setting exit code (error level) to break make process
\count255=\interactionmode
\batchmode

% zh-cn main font
\let\cnmainft=\macmainfont
\font\thefont="\cnmainft"\space at 10pt
\ifx\thefont\nullfont
  \let\cnmainft=\winmainfont
  \font\thefont="\cnmainft"\space at 10pt
  \ifx\the\nullfont
    \let\cnmainft=\linuxmainfont
    \font\thefont="\cnmainft"\space at 10pt
    \ifx\the\nullfont
      \errorstopmode
      \errmessage{no suitable Chinese main font found}
    \fi
  \fi
\fi

% zh-cn bold font
\let\cnboldft=\macboldfont
\font\thefont="\cnboldft"\space at 10pt
\ifx\thefont\nullfont
  \let\cnboldft=\winboldfont
  \font\thefont="\cnboldft"\space at 10pt
  \ifx\the\nullfont
    \let\cnboldft=\linuxboldfont
    \font\thefont="\cnboldft"\space at 10pt
    \ifx\the\nullfont
      \errorstopmode
      \errmessage{no suitable Chinese bold font found}
    \fi
  \fi
\fi

% mono font
\let\monoft=\macmonofont
\font\thefont="\monoft"\space at 10pt
\ifx\thefont\nullfont
  \let\monoft=\winmonofont
  \font\thefont="\monoft"\space at 10pt
  \ifx\the\nullfont
    \let\monoft=\linuxmonofont
    \font\thefont="\monoft"\space at 10pt
    \ifx\the\nullfont
      \errorstopmode
      \errmessage{no suitable mono font found}
    \fi
  \fi
\fi

\interactionmode=\count255

\titleformat{\paragraph}
{\normalfont\normalsize\bfseries}{\theparagraph}{1em}{}
\titlespacing*{\paragraph}
{0pt}{3.25ex plus 1ex minus .2ex}{1.5ex plus .2ex}

% for literate Haskell code
\lstdefinestyle{Haskell}{
  flexiblecolumns=false,
  basewidth={0.5em,0.45em},
  morecomment=[l]--,
  literate={+}{{$+$}}1 {/}{{$/$}}1 {*}{{$*$}}1 {=}{{$=$}}1
           {>}{{$>$}}1 {<}{{$<$}}1 {\\}{{$\lambda$}}1
           {\\\\}{{\char`\\\char`\\}}1
           {->}{{$\rightarrow$}}2 {>=}{{$\geq$}}2 {<-}{{$\leftarrow$}}2
           {<=}{{$\leq$}}2 {=>}{{$\Rightarrow$}}2
           {\ .}{{$\circ$}}2 {\ .\ }{{$\circ$}}2
           {>>}{{>>}}2 {>>=}{{>>=}}2
           {|}{{$\mid$}}1
}

% "define" Scala
\lstdefinelanguage{Scala}{
  morekeywords={abstract,case,catch,class,def,%
    do,else,extends,false,final,finally,%
    for,if,implicit,import,match,mixin,%
    new,null,object,override,package,%
    private,protected,requires,return,sealed,%
    super,this,throw,trait,true,try,%
    type,val,var,while,with,yield},
  otherkeywords={=>,<-,<\%,<:,>:,\#,@},
  sensitive=true,
  morecomment=[l]{//},
  morecomment=[n]{/*}{*/},
  morestring=[b]",
  morestring=[b]',
  morestring=[b]"""
}

\lstloadlanguages{C, C++, Java, Lisp, Haskell, Python, Scala}

\lstset{
  basicstyle=\small\ttfamily,
  commentstyle=\rmfamily,
  texcl=true,
  showstringspaces = false,
  upquote=true,
  flexiblecolumns=false
}

\newcommand\doubleplus{\ensuremath{\mathbin{+\mkern-10mu+}}}
%\newcommand\doubleplus{+\kern-1.3ex+\kern0.8ex}
%\newcommand\mdoubleplus{\ensuremath{\mathbin{+\mkern-10mu+}}}
\newcommand{\arrowto}[2][]{\xlongrightarrow[#1]{\displaystyle #2}}
\newcommand{\lbb}{\llparenthesis} % left banana bracket
\newcommand{\rbb}{\rrparenthesis} % right banana bracket

\newcommand{\lbparen}{%
  \mathopen{%
    \sbox0{$()$}%
    \setlength{\unitlength}{\dimexpr\ht0+\dp0}%
    \raisebox{-\dp0}{%
      \begin{picture}(.32,1)
      \linethickness{\fontdimen8\textfont3}
      \roundcap
      \put(0,0){\raisebox{\depth}{$($}}
      \polyline(0.32,0)(0,0)(0,1)(0.32,1)
      \end{picture}%
    }%
  }%
}

\newcommand{\rbparen}{%
  \mathclose{%
    \sbox0{$()$}%
    \setlength{\unitlength}{\dimexpr\ht0+\dp0}%
    \raisebox{-\dp0}{%
      \begin{picture}(.32,1)
      \linethickness{\fontdimen8\textfont3}
      \roundcap
      \put(-0.08,0){\raisebox{\depth}{$)$}}
      \polyline(0,0)(0.32,0)(0.32,1)(0,1)
      \end{picture}%
    }%
  }%
}

\newcommand{\llb}{\lbparen} % left lens bracket
\newcommand{\rlb}{\rbparen} % right lens bracket

% ======================================================================

\def\BibTeX{{\rm B\kern-.05em{\sc i\kern-.025em b}\kern-.08em
    T\kern-.1667em\lower.7ex\hbox{E}\kern-.125emX}}

%
% mathematics
%
\newcommand{\be}{\begin{equation}}
\newcommand{\ee}{\end{equation}}

% numbered equation array
\newcommand{\bea}{\begin{eqnarray}}
\newcommand{\eea}{\end{eqnarray}}

% equation array not numbered
\newcommand{\bean}{\begin{eqnarray*}}
\newcommand{\eean}{\end{eqnarray*}}

% begin/end reasoning
\newcommand{\bre}{\[ \begin{array}{rcll}}
\newcommand{\ere}{\end{array} \]}

% begin/end long reasoning
\newcommand{\blre}{\[ \begin{array}{cll}}
\newcommand{\elre}{\end{array} \]}

\newcommand{\btab}{\vspace{5mm} \begin{tabular}}
\newcommand{\etab}{\end{tabular} \vspace{5mm}}

\makeatletter
\newcommand{\verbatimfont}[1]{\renewcommand{\verbatim@font}{\ttfamily#1}}
\makeatother

\setcounter{tocdepth}{4}
%\setcounter{secnumdepth}{4}

%\verbatimfont{\footnotesize}

\DeclareOption{cn}{
  \setmainfont[Mapping=tex-text, BoldFont=\cnboldft]{\cnmainft} % 正文字体（衬线字体）对应Roman
  \setsansfont[BoldFont=\cnboldft]{\cnmainft}   % 无衬线字体
  \setmonofont[Scale=MatchLowercase]{\monoft}   % 等宽字体

  \XeTeXlinebreaklocale "zh"  % to solve the line breaking issue
  \XeTeXlinebreakskip = 0pt plus 1pt minus 0.1pt

  \newtheorem{axiom}{公理}[section]
  \newtheorem{theorem}{定理}[section]
  \newtheorem{lemma}[theorem]{引理}
  \newtheorem{proposition}[theorem]{命题}
  \newtheorem{corollary}[theorem]{推论}
  \newtheorem{definition}{定义}[section]
  \newtheorem{example}{例}[section]

  % 中文书籍设置
  % ====================================
  \renewcommand\ExerciseName{练习}
}

\DeclareOption{en}{
  \newtheorem{axiom}{Axiom}[section]
  \newtheorem{theorem}{Theorem}[section]
  \newtheorem{lemma}[theorem]{Lemma}
  \newtheorem{proposition}[theorem]{Proposition}
  \newtheorem{corollary}[theorem]{Corollary}
  \newtheorem{definition}{Definition}[section]
  \newtheorem{example}{Example}[section]
}


\DeclareOption*{\PackageWarning{prelude}{Unknown ‘\CurrentOption’}}
\ProcessOptions\relax
